package debtbundler.presentation.balancesheeteditor;

import java.awt.Color;
import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;

import debtbundler.domain.entity.SharedCost;
import debtbundler.presentation.VAlignment;
import debtbundler.presentation.basecomponents.DomainViewComponent;
import debtbundler.presentation.basecomponents.SelectableComponent;
import debtbundler.presentation.utils.IconConstants;
import debtbundler.presentation.utils.UIUtils;

/**
 * Calculates the cost value and balance of transaction values for a given SharedCost,
 * highlighting the value when unbalanced. 
 * 
 * @author Anssi
 *
 */
public class SharedCostBalancePanel extends SelectableComponent implements DomainViewComponent<SharedCost> {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(SharedCostBalancePanel.class.getName());
	
	private SharedCost sharedCost;
	
	protected VAlignment vAlignment;
	protected JLabel costValueFieldLabel;
	protected JFormattedTextField costValueField;
	protected JLabel balanceFieldLabel;
	protected JFormattedTextField balanceField;

	
	protected SharedCostBalancePanel(SharedCost pool) {
		this(pool, VAlignment.CENTER);
	}
	
	protected SharedCostBalancePanel(SharedCost sharedCost, VAlignment vAlignment) {
		if (sharedCost == null) {
			throw new IllegalArgumentException("Null sharedCost not allowed");
		}
		this.sharedCost = sharedCost;
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setVerticalAlignment(vAlignment);
	}	
	
	protected void createGUI() {
		// Components
		costValueField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
		costValueField.setEditable(false);
		costValueField.setMaximumSize( new Dimension(costValueField.getMaximumSize().width, costValueField.getPreferredSize().height) );
		costValueFieldLabel = new JLabel(UIUtils.getImageIcon(IconConstants.TOTAL_COST_16));
		costValueFieldLabel.setLabelFor(costValueField);
		
		balanceField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
		balanceField.setEditable(false);
		balanceField.setMaximumSize( new Dimension(balanceField.getMaximumSize().width, balanceField.getPreferredSize().height) );
		balanceFieldLabel = new JLabel(UIUtils.getImageIcon(IconConstants.BALANCE_16));
		balanceFieldLabel.setLabelFor(balanceField);
		
		// Layout
		if (vAlignment == VAlignment.BOTTOM || vAlignment == VAlignment.CENTER) {
			this.add(Box.createVerticalGlue());
		}
		
		Box totalCostBox = Box.createHorizontalBox();
		totalCostBox.add(costValueFieldLabel);
		totalCostBox.add(Box.createRigidArea(new Dimension(6, 0)));
		totalCostBox.add(costValueField);
		totalCostBox.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
		this.add(totalCostBox);
		
		Box balanceBox = Box.createHorizontalBox();
		balanceBox.add(balanceFieldLabel);
		balanceBox.add(Box.createRigidArea(new Dimension(6, 0)));
		balanceBox.add(balanceField);
		balanceBox.setBorder(BorderFactory.createEmptyBorder(0, 6, 6, 6));
		this.add(balanceBox);
		
		if (vAlignment == VAlignment.TOP || vAlignment == VAlignment.CENTER) {
			this.add(Box.createVerticalGlue());
		}
	}
	
	/**
	 * Sets the vertical alignment of fields in this component.
	 * 
	 * @param vAlignment	the vertical alignment
	 */
	public void setVerticalAlignment(VAlignment vAlignment) {
		this.vAlignment = vAlignment != null ? vAlignment : VAlignment.CENTER;
		this.removeAll();
		createGUI();
		refreshDomainFields();
		this.validate();
	}

	@Override
	public void refreshDomainFields() {
		this.costValueField.setValue(this.sharedCost.getValue());
		
		double balance = this.sharedCost.getBalance();
		balanceField.setValue( balance );
		boolean isBalanced = sharedCost.isBalanced();
		if (!isBalanced) {
			balanceField.setBackground(Color.RED);
		} else {
			balanceField.setBackground(null);
		}
		
		logger.log(Level.FINER, "Refreshed values of model fields for sharedCost {0}", this.sharedCost);
	}

	@Override
	public SharedCost getDomainComponent() {
		return this.sharedCost;
	}	
	
}
